﻿using Liang.Resource;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Liang.UI
{
    public class LeftBar : Panel
    {
        private int buttonHeight;
        private int selectedBand;
        private int selectedBandHeight;

        public int ButtonHeight
        {
            get
            {
                return buttonHeight;
            }

            set
            {
                buttonHeight = value;
            }
        }

        public int SelectedBand
        {
            get
            {
                return selectedBand;
            }
            set
            {
                SelectBand(value);
            }
        }

        public LeftBar()
        {
            buttonHeight = 40;
            selectedBand = 0;
            selectedBandHeight = 0;
        }

        public void Initialize()
        {
            this.BorderStyle = BorderStyle.None;
            Parent.SizeChanged += new EventHandler(SizeChangedEvent);
        }

        /**
         * 添加子控件
         * caption      权限(菜单)名称
         * imageUrl     背景图片路径
         * LeftIcon     
         * Image        背景图片
         * onClickEvent 事件
         * 
         * */
        public void AddBand(string caption, string imageUrl, Image image, EventHandler onClickEvent)
        {
            LeftIcon content = new LeftIcon();
            content.leftBar = this;
            content.addImg(caption, image, onClickEvent, imageUrl);
            Controls.Add(content);
        }

        public void ClearBand()
        {
            Controls.Clear();
        }

        public void SelectBand(int index)
        {
            selectedBand = index;
            RedrawBands();
        }

        private void RedrawBands()
        {
            for (int i = 0; i < Controls.Count; i++)
            {
                LeftIcon icon = Controls[i] as LeftIcon;
                RecalcLayout(icon, i);
            }
        }


        private void RecalcLayout(LeftIcon icon, int index)
        {
            buttonHeight = 50;
            int vPos = (index <= selectedBand) ? buttonHeight * index : buttonHeight * index + selectedBandHeight;
            int height = selectedBand == index ? selectedBandHeight + buttonHeight : buttonHeight;
            icon.Location = new Point(0, vPos);
            icon.Size = new Size(ClientRectangle.Width, 50);
        }

        private void SizeChangedEvent(object sender, EventArgs e)
        {
            Size = new Size(Size.Width, ((Control)sender).ClientRectangle.Size.Height);
            RedrawBands();
        }
    }

    public class LeftIcon : PictureBox
    {
        public LeftBar leftBar;
        private Color bckgColor;
        private bool mouseEnter;

        public void addImg(string caption, Image image, EventHandler onClickEvent, string imageUrl)
        {
            bckgColor = Color.White;
            Tag = imageUrl;
            Name = caption;
            Cursor = Cursors.Hand;
            Click += onClickEvent;
            SizeMode = PictureBoxSizeMode.StretchImage;
            Image = image;
            mouseEnter = false;
            //MouseEnter += new EventHandler(OnMouseEnter);
            //MouseLeave += new EventHandler(OnMouseLeave);
            //MouseMove += new MouseEventHandler(OnMouseMove);
            MouseDown += new MouseEventHandler(OnMouseDown);

        }

        private void OnMouseMove(object sender, MouseEventArgs args)
        {
            if ((args.X < Size.Width - 2) &&
                (args.Y < Size.Width - 2) &&
                (!mouseEnter))
            {
                BackColor = Color.LightCyan;
                Location = Location - new Size(1, 1);
                mouseEnter = true;
            }
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            if (mouseEnter)
            {
                BackColor = bckgColor;
                Location = Location + new Size(1, 1);
                mouseEnter = false;
            }
        }

        private void OnMouseDown(object sender, EventArgs s)
        {
            PictureBox box = sender as PictureBox;
            string tag = box.Tag.ToString();
            if (tag != null && !"".Equals(tag) && tag.IndexOf("down") >= 0)
            {
                box.Image = AssemblyHelper.GetImage("LiangResource", tag.Replace("down", "chose"));
            }

            if(box.Parent != null)
            {
                //遍历本窗体中所有的ComboBox控件
                foreach (Control control in box.Parent.Controls)
                {
                    PictureBox childBox = control as PictureBox;
                    if (!childBox.Name.Equals(box.Name))
                    {


                        if (childBox.Tag != null && !"".Equals(childBox.Tag))
                        {
                            childBox.Image = AssemblyHelper.GetImage("LiangResource", childBox.Tag.ToString().Replace("chose", "down"));
                        }
                    }
                }
            }
        }
    }
}
